package cn.ncu.service;

import cn.ncu.dao.PlaceDao;
import cn.ncu.entity.Place;
import cn.ncu.entity.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:Martina
 * @create:2020-07-16 14:45
 * @description:
 */
@Service
@Transactional
public class PlaceServiceImpl implements PlaceService{

    @Autowired
    private PlaceDao placeDao;

    @Autowired
    private ProvinceService provinceService;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Place> findByProvinceIdPage(Integer page, Integer rows, String provinceId) {
        int start = (page-1)*rows;
        return placeDao.findByProvinceIdPage(start,rows,provinceId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer findByProvinceIdCounts(String provinceId) {
        return placeDao.findByProvinceIdCounts(provinceId);
    }

    @Override
    public void save(Place place) {
        Province province = provinceService.findOne(place.getProvinceid());
        province.setPlacecounts(province.getPlacecounts()+1);
        place.setProvinceid(province.getId());
        placeDao.save(place);
    }

    @Override
    public List<String> findProvinces() {
        return placeDao.findProvinces();
    }

    @Override
    public void delete(String id) {
        Place place = placeDao.findAll(id);
        Province province = provinceService.findAll(place.getProvinceid());
        province.setPlacecounts(String.valueOf(Integer.valueOf(province.getPlacecounts())-1));
        placeDao.delete(id);
    }

    @Override
    public void update(Place place) {
        placeDao.update(place);
    }

    @Override
    public Place findAll(String id) {
        return placeDao.findAll(id);
    }


}
